from django.db import models

from django.db.models import permalink
from django.contrib.auth.models import User
from settings import IMAGE_DIR
import re


class Text(models.Model):
	
	created = models.DateTimeField(auto_now_add=True)
	text = models.TextField(blank=True,null=True)
	modify_comment = models.CharField(max_length=40,blank=True,null=True)
	modify_user = models.ForeignKey(User,blank=True,null=True)
	modify_ip = models.IPAddressField(blank=True,null=True)
	
	def save(self):
		categories = False
		try:
			categories = re.findall('\[\[Category:([^\]|]*)\]\]',self.text)
			
		except:pass
				
		models.Model.save(self)
		
		return categories
	
	def __str__(self):
	        return self.text[:20]

	class Admin:		
		search_fields = ('text','created')
	
	class Meta:
		ordering = ['-created',]	


class History(models.Model):
	text_id = models.ManyToManyField(Text,core=True)
	

class Category(models.Model):
	slug = models.SlugField(
		'Slug',
		prepopulate_from=("title",),
		help_text='Automatically built from the title.'	,
		unique=True
	)
	
	title = models.CharField(max_length=100)
	create_date = models.DateField(auto_now_add=True,blank=True,null=True)
	

	def get_absolute_url(self):
		return "/wiki/category/show/%s/" % self.slug
	


	def __str__(self):
	        return self.title


	class Admin:		
		search_fields = ('name',)

class Page(models.Model):
	slug = models.SlugField(
		'Slug',
		prepopulate_from=("title",),
		help_text='Automatically built from the title.'	,
		unique='True'
	)
	
	title = models.CharField(max_length=100)
	create_date = models.DateTimeField(auto_now_add=True)
	edit_date = models.DateTimeField(auto_now=True)
	category = models.ManyToManyField(Category,blank=True,null=True)
	current_text = models.ForeignKey(Text)
	history = models.ForeignKey(History)

	
	current_talk = models.ForeignKey(Text,blank=True,null=True,related_name="current_talk",)
	talk_history = models.ForeignKey(History,blank=True,null=True,related_name='talk_history')

	
	
	def get_absolute_url(self):
		return "/wiki/show/%s/"%self.slug
	
	#@permalink
	#def get_edit_url(self):
		#return ('wiki.views.update_object',(),{'slug':self.slug})
	def get_edit_url(self):
		return "/wiki/edit/%s/"%self.slug
	#@permalink
	#def get_history_url(self):
		#return ('wiki.views.history',(),{'slug':self.slug})
	def get_history_url(self):
		return "/wiki/history/%s/"%self.slug
	
	def __str__(self):
	        return self.title

	class Admin:		
		search_fields = ('title',)
		



class File(models.Model):
	slug = models.SlugField(
		'Slug',
		prepopulate_from=("name",),
		help_text='Automatically built from the name.'	,
	)
	name = models.ImageField(upload_to=IMAGE_DIR+"/contributed/")
	create_date = models.DateTimeField(auto_now_add=True)
	modify_date = models.DateTimeField(auto_now=True)

	
	def get_absolute_url(self):
                  return "/images/%s" % (self.name.replace(IMAGE_DIR,''))

	def __str__(self):
	        return self.name


	class Admin:		
		search_fields = ('name',)







